<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-05-29
 * Time: 14:32
 * 酒店房间基本信息控制类
 */

namespace app\api\controller\v1;


use app\api\model\Basis as BasisModel;
use think\facade\Request;

class Basis
{
    /**
     * 获取全部
     */
    public function getDataAll()
    {
        return BasisModel::order('order_sort desc')
            ->select()->toArray();
    }
    /**
     * 获取列表
     * @validate('PagingParameter')
     * @auth('酒店基本信息','酒店管理')
     */
    public function getDataList($page,$limit,$name)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        $query = BasisModel::where($where)->order('order_sort desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return BasisModel::with('img')->where('id',$id)->find();
    }

    /**
     * 创建分类
     * @validate('CheckBasis')
     */
    public function createData()
    {
        $data = Request::post();
        BasisModel::create($data);
        return writeJson(201, [], '添加成功！');
    }

    /**
     * 更新信息
     * @validate('CheckBasis')
     */
    public function updateData()
    {
        $data = Request::put();
        $mode = new BasisModel();
        $mode->allowField(true)->save($data,['id',$data['id']]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 删除信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        BasisModel::destroy($id);
        return writeJson(203, [], '删除成功！');
    }
}